<?php
namespace Home\Controller;
use Think\Controller;
class AddpersonController extends Controller {
    // 后台登录页
    public function index(){
        if(isset($_SESSION['admin'])){ 
            $position = D('position');
            $positionList = $position->select();            
            $this->assign('arrP',$positionList);

            $sector = D('sector');
            $sectorList = $sector->select();            
            $this->assign('arrS',$sectorList);

            $this->display();
        }else{
            $url = 'http://localhost/person/index.php/Home/Index/manage';
            header("location: $url");
        }
    }

     //添加员工
    public function personAdd($number,$name,$age,$sex,$phone,$address,$salary,$sectorId,$positionId){
        if($number == null || $name  == null || $age  == null|| $sex  == null|| $phone == null|| $address == null|| $positionId == null){
            $result = array(
                'code'=> '1',
                'ext'=> 'error'
            );
            echo json_encode($result);
            return;
        }
        $position = D("position");
        $salaryP = $position->where("id='$positionId'")->field('salary')->select();
        $salary = $salaryP[0]['salary']; 
        $person = D("person");
        $data['number'] = $number;
        $data['name'] = $name;
        $data['age'] = $age;
        $data['sex'] = $sex;
        $data['phone'] = $phone;
        $data['address'] = $address;
        $data['salary'] = $salary;
        $data['sector_id'] = $sectorId;
        $data['position_id'] = $positionId;
         if($person->create($data)){
            $id = $person->add();
            if($id){
                $result = array(
                    'code' => '0',
                    'ext' => 'success'
                );
                echo json_encode($result);
            }
        }
    }
      //退出
    public function doLogout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(),'',time()-1,'/');
        }
        session_destroy();
        $this->redirect('Index/index');
    }



}
